package com.key.win.system.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.key.win.system.model.SysOrgan;
import com.key.win.system.model.SysOrganUser;
import com.key.win.system.model.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;


/**
 * 用户机构关系
 */
@Mapper
public interface SysOrganUserDao extends BaseMapper<SysOrganUser> {

    int deleteOrganUser(@Param("userId") String userId, @Param("organId") String organId);

    /**
     * 根据用户id获取Organ
     *
     * @param userId
     * @return
     */
    @Select("select o.*,ou.administrative_permissions from sys_organ_user ou inner join sys_organ o on ou.organ_id = o.id where ou.user_id = #{userId} and ou.enable_flag = 1 and o.enable_flag = 1")
    List<SysOrgan> findOrganByUserId(String userId);

    /**
     * 根据用户id获取Organ
     *
     * @param organId
     * @return
     */
    @Select("select u.* from sys_organ_user ou inner join sys_user u on ou.user_id = u.id where ou.organ_id = #{organId} and ou.enable_flag = 1 and u.enable_flag = 1")
    List<SysUser> findUserByOrganId(String organId);

    /**
     * 根据用户ids 获取
     *
     * @param userIds
     * @return
     */

    @Select("<script>select r.* from sys_organ_user ou inner join sys_organ r on r.id = ou.organ_id where  r.enable_Flag = 1 and ou.enable_Flag = 1 and ou.user_id IN " +
            " <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> " +
            " #{item} " +
            " </foreach>" +
            "</script>")
    List<SysOrgan> findOrganByUserIds(List<String> userIds);

    /**
     * 根据用户ids 获取
     *
     * @param organIds
     * @return
     */

    @Select("<script>select u.* from sys_user_organ ou inner join sys_user u on u.id = ou.user_id where u.enable_Flag = 1 and ou.enable_Flag = 1  and ou.organ_id IN " +
            " <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> " +
            " #{item} " +
            " </foreach>" +
            "</script>")
    List<SysUser> findUserByOrganIds(List<String> organIds);

    void saveBatchUserIdAndOrganIds(@Param("userId") String userId, @Param("organIds") Set<String> organIds);

    void saveBatchUserIdsAndOrganId(@Param("userIds") Set<String> userIds, @Param("organId") String organId);

    void saveBatchUserOrgans(@Param("userId") String userId, @Param("sysOrganUser") List<SysOrganUser> sysOrganUser);

    void saveBatchUsersOrgan(@Param("sysOrganUser") List<SysOrganUser> sysOrganUser, @Param("organId") String organId);

    /**
     * 根据用户id获取组信息
     *
     * @param organId
     * @return
     */
    @Select("select u.* from sys_organ_user ou inner join sys_user u on u.id = ou.user_id where ou.organ_id = #{organId} and u.enable_flag = 1 and ou.enable_flag = 1")
    List<SysUser> findUserPagedByOrganId(IPage<SysUser> page, String organId);

}
